<?php
$strPageTitle = "SQL Man";
include("../qcubed.inc.php");
include(__APPS_INCLUDE__."/header.inc.php");


//-------------------Configure below
 $host = "localhost";
 $user = "root";
 $pass = "root";
//-------------------------------------
 $dbCreateFile="db/crm-sqleditor-sql.sql";
 $dbDummyFile= "db/dummy-data.sql";
 $dbExampleFile= "db/qcubed-example.sql";
 $dbEnumFile="db/sql_enum_types.sql";

   $sqlErrorText = '';
   $sqlErrorCode = 0;
   $sqlStmt      = '';
   $sqlFileToExecute = 'test.sql';
   // $sqlFileToExecute = "../crmSQLTemplate.sql";
   //$sqlFileToExecute = "../test.sql";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>MySQL Executor</title>
</head>
<body>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

     <h3> <font color="red">WARNING CREATING A DATABASE WILL DROP THE CURRENT DB WITH ALL DATA</font></h3>
     <br/>
     <br/>
  <table width="100%">
   <tr>
    <td>DataBaseName:</td>
           <td>
           <select name="database">
               <option value="crm-ev-dev">crm-ev-dev</option>
               <option value="crm-ev-test">crm-ev-test</option>
               <option value="Qexamples">Qexamples</option>
           </select>

       </td>
   </tr>
   <tr>
       <td>Create DB or Insert data</td>
       <td>

           
           <select name="sqlfile">
               <option value="<?php _p($dbDummyFile)?>">Dummy Data</option>
               <option value="<?php _p($dbCreateFile)?>">Create DB</option>
               <option value="<?php _p($dbExampleFile)?>">Qcubed Examples DB</option>

           </select>

       </td>
       </tr>
    
     
  </table>
     <input type="submit" name="submitBtn" value="Install" />
 </form>

<?Php
 if (isset($_POST['submitBtn'])){
    $sqlFileToExecute = isset($_POST['sqlfile'])? $_POST['sqlfile'] :'';
    $dbname = isset($_POST['database'])? $_POST['database'] :'';
    
   echo "Running ...   " . $sqlFileToExecute  ."<br/><br/>";

   $con = mysql_connect($host,$user,$pass);
   if ($con !== false){
       $sqlcmd="";
        if ($sqlFileToExecute == $dbCreateFile ||  $sqlFileToExecute ==$dbExampleFile){
            $sqlcmd  = "DROP DATABASE IF EXISTS `". $dbname . "`;";
            $sqlcmd = $sqlcmd ." CREATE DATABASE `". $dbname . "`;" ;
        }
        $sqlcmd=$sqlcmd.   " use `".$dbname."`;";

         // Load and explode the sql file
        $f = fopen($sqlFileToExecute,"r+");
        $sqltmp =  fread($f,filesize($sqlFileToExecute));

        //Add types
        $sqltmp2="";
        if ($sqlFileToExecute == $dbCreateFile){
              $ftype = fopen($dbEnumFile,"r+");
              $sqltmp2 =  fread($ftype,filesize($dbEnumFile));
        }
        $sqlFile = $sqlcmd . $sqltmp. $sqltmp2;
        $sqlArray =  explode(';',$sqlFile);
   }
   echo $sqlcmd . "<br><br>";



       foreach ($sqlArray as $stmt) {
           if (strlen($stmt)>3){
                $result = mysql_query($stmt);
                  if (!$result){
                     $sqlErrorCode = mysql_errno();
                     $sqlErrorText = mysql_error();
                     $sqlStmt      = $stmt;
                     break;
                  }
               }
          }
  
   if ($sqlErrorCode == 0){
      echo "<tr><td>Installation was finished succesfully!</td></tr>";
      echo "<tr><td><font color='red'><b>If you update the SQL dont forget to RUN CODEGEN</b></font></td></tr>";
   } else {
      echo "<tr><td>An error occured during installation!</td></tr>";
      echo "<tr><td>Error code: $sqlErrorCode</td></tr>";
      echo "<tr><td>Error text: $sqlErrorText</td></tr>";
      echo "<tr><td>Statement:<br/> $sqlStmt</td></tr>";
   }


   }
   function run_mysql($quary){

   }
?>

    </body>
</html>